package com.wwwarehouse.contract.contract;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ClearEditText;
import com.wwwarehouse.common.custom_widget.textinput.TextInputLayout;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.DeliverableItemBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.DeliveryItemEvent;
import com.wwwarehouse.contract.event.RefreshPublishGoodsEvent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

import static contract.wwwarehouse.com.contract.R.string.early_time;

/**
 * 交付项Fragment
 * Created by ShineF on 2017/10/17 0017.
 */

public class ReleaseDeliveryItemFragment extends TheParentFragment implements View.OnClickListener {
	private ClearEditText mEarliestDeliveryTimeEdt;
	private ClearEditText mEarliestCommitTimeEdt;
	private ClearEditText mLatestDeliveryTimeEdt;
	private ClearEditText mLatestCommitTimeEdt;
	private TextInputLayout mEarliestDeliveryTimeLayout;
	private TextInputLayout mEarliestCommitTimeLayout;
	private TextInputLayout mLatestDeliveryTimeLayout;
	private TextInputLayout mLatestCommitTimeLayout;
	private boolean isEarliestDeTime;
	private boolean isEarliestComTime;
	private boolean isLatestDeTime;
	private boolean isLatestComTime;
	private Button mConfirmBtn;
	private String deliveryTempUkid;
	private String pbUkid;
	private boolean isCreate;
	private String mContractUkid;


	@Override
	protected int getContentId() {
		return R.layout.fragment_release_delivery_item;
	}

	@Override
	protected void init(View view) {
		mEarliestDeliveryTimeEdt = (ClearEditText) view.findViewById(R.id.earliest_delivery_time_edt);
		mEarliestCommitTimeEdt = (ClearEditText) view.findViewById(R.id.earliest_commit_time_edt);
		mLatestDeliveryTimeEdt = (ClearEditText) view.findViewById(R.id.latest_delivery_time_edt);
		mLatestCommitTimeEdt = (ClearEditText) view.findViewById(R.id.latest_commit_time_edt);

		mEarliestDeliveryTimeLayout = (TextInputLayout) view.findViewById(R.id.earliest_delivery_time_layout);
		mEarliestCommitTimeLayout = (TextInputLayout) view.findViewById(R.id.earliest_commit_time_layout);
		mLatestDeliveryTimeLayout = (TextInputLayout) view.findViewById(R.id.latest_delivery_time_layout);
		mLatestCommitTimeLayout = (TextInputLayout) view.findViewById(R.id.latest_commit_time_layout);

		mConfirmBtn = (Button) view.findViewById(R.id.confirm_btn);
		mConfirmBtn.setOnClickListener(this);
		bindEdt();

	}

	private void checkCanClick() {
		if (StringUtils.isNoneNullString(mEarliestDeliveryTimeEdt.getText().toString().trim()) &&
				StringUtils.isNoneNullString(mEarliestCommitTimeEdt.getText().toString().trim()) &&
				StringUtils.isNoneNullString(mLatestDeliveryTimeEdt.getText().toString().trim()) &&
				StringUtils.isNoneNullString(mLatestCommitTimeEdt.getText().toString().trim())) {
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_normal_shape));
		} else {
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
		}
	}

	private void bindEdt() {
		mEarliestDeliveryTimeEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				checkCanClick();
//				try {
//					if (StringUtils.isNullString(s.toString())) {
//						mEarliestDeliveryTimeLayout.setStateNormal();
//						isEarliestDeTime = false;
//						if (mLatestDeliveryTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//							mLatestDeliveryTimeLayout.setStateNormal();
//						}
//						checkCanClick();
//						return;
//					} else {
//						if (Long.valueOf(s.toString()) == 0) {
//							mEarliestDeliveryTimeLayout.setStateWrong("发布时间不能为0");
//							isEarliestDeTime = false;
//							checkCanClick();
//							return;
//						}
//					}
//					long earDetime = 0;
//					long lateDetime = 0;
//					if (StringUtils.isNoneNullString(mEarliestDeliveryTimeEdt.getText().toString().trim())) {
//						earDetime = Long.valueOf(mEarliestDeliveryTimeEdt.getText().toString().trim());
//					}
//					if (StringUtils.isNoneNullString(mLatestDeliveryTimeEdt.getText().toString().trim())) {
//						lateDetime = Long.valueOf(mLatestDeliveryTimeEdt.getText().toString().trim());
//					}
//					if (lateDetime == 0) {
//						mEarliestDeliveryTimeLayout.setStateNormal();
//						isEarliestDeTime = true;
//					} else {
//						if (earDetime >= lateDetime) {
//							mEarliestDeliveryTimeLayout.setStateWrong("最晚时间要大于最早时间");
//							isEarliestDeTime = false;
//						} else {
//							mEarliestDeliveryTimeLayout.setStateNormal();
//							isEarliestDeTime = true;
//							if (mLatestDeliveryTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//								mLatestDeliveryTimeLayout.setStateNormal();
//								isLatestDeTime = true;
//							}
//						}
//					}
//					checkCanClick();
//				} catch (NumberFormatException e) {
//					e.printStackTrace();
//				}
			}
		});

		mLatestDeliveryTimeEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				checkCanClick();

//				try {
//					if (StringUtils.isNullString(s.toString())) {
//						mLatestDeliveryTimeLayout.setStateNormal();
//						isLatestDeTime = false;
//						if (mEarliestDeliveryTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//							mEarliestDeliveryTimeLayout.setStateNormal();
//						}
//						if (mEarliestCommitTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//							mEarliestCommitTimeLayout.setStateNormal();
//							isEarliestComTime = true;
//						}
//						checkCanClick();
//						return;
//
//					} else {
//						if (Long.valueOf(s.toString()) == 0) {
//							mLatestDeliveryTimeLayout.setStateWrong("发布时间不能为0");
//							isLatestDeTime = false;
//							checkCanClick();
//							return;
//						}
//
//					}
//					long earlyDeTime = 0;
//					long lateDeTime = 0;
//					long earlyComitTime = 0;
//					if (StringUtils.isNoneNullString(mEarliestDeliveryTimeEdt.getText().toString().trim())) {
//						earlyDeTime = Long.valueOf(mEarliestDeliveryTimeEdt.getText().toString().trim());
//					}
//					if (StringUtils.isNoneNullString(mLatestDeliveryTimeEdt.getText().toString().trim())) {
//						lateDeTime = Long.valueOf(mLatestDeliveryTimeEdt.getText().toString().trim());
//					}
//					if (StringUtils.isNoneNullString(mEarliestCommitTimeEdt.getText().toString().trim())) {
//						earlyComitTime = Long.valueOf(mEarliestCommitTimeEdt.getText().toString().trim());
//					}
//
//					if (earlyComitTime == 0) {
//						if (earlyDeTime == 0) {
//							mLatestDeliveryTimeLayout.setStateNormal();
//							isLatestDeTime = true;
//						} else {
//							if (earlyDeTime >= lateDeTime) {
//								mLatestDeliveryTimeLayout.setStateWrong("最晚时间要大于最早时间");
//								isLatestDeTime = false;
//							} else {
//								mLatestDeliveryTimeLayout.setStateNormal();
//								isLatestDeTime = true;
//								if (mEarliestDeliveryTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//									mEarliestDeliveryTimeLayout.setStateNormal();
//									isEarliestDeTime = true;
//								}
//							}
//						}
//					} else {
//						if (earlyComitTime <= lateDeTime) {
//							//报错
//							mLatestDeliveryTimeLayout.setStateWrong("交付时间要大于发货时间");
//							isLatestDeTime = false;
//						} else {
//							//再比较和最早时间
//							if (earlyDeTime == 0) {
//								mEarliestCommitTimeLayout.setStateNormal();
//								isEarliestComTime = true;
//								if (mEarliestCommitTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//									mEarliestCommitTimeLayout.setStateNormal();
//									isEarliestComTime = true;
//								}
//							} else {
//								if (earlyDeTime < lateDeTime) {
//									mLatestDeliveryTimeLayout.setStateNormal();
//									isLatestDeTime = true;
//									if (mEarliestDeliveryTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//										mLatestCommitTimeLayout.setStateNormal();
//										isLatestComTime = true;
//									}
//									if (mEarliestCommitTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//										mEarliestCommitTimeLayout.setStateNormal();
//										isEarliestComTime = true;
//									}
//								} else {
//									mLatestDeliveryTimeLayout.setStateWrong("最晚时间要大于最早时间");
//									isLatestDeTime = false;
//								}
//							}
//
//						}
//					}
//
//
//					checkCanClick();
//				} catch (NumberFormatException e) {
//					e.printStackTrace();
//				}

			}
		});

		mEarliestCommitTimeEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				checkCanClick();

//				try {
//					if (StringUtils.isNullString(s.toString())) {
//						mEarliestCommitTimeLayout.setStateNormal();
//						isEarliestComTime = false;
//						checkCanClick();
//						return;
//					} else {
//						if (Long.valueOf(s.toString()) == 0) {
//							mEarliestCommitTimeLayout.setStateWrong("发布时间不能为0");
//							isEarliestComTime = false;
//							checkCanClick();
//							return;
//						}
//					}
//					long lastDeTime = 0; //最晚发货时间
//					long earlyComitTime = 0; //最早交付时间
//					long lastComitTime = 0;//最晚交付时间
//
//					//优先比较最早交付时间与最晚发货时间
//					if (StringUtils.isNoneNullString(mLatestDeliveryTimeEdt.getText().toString().trim())) {
//						lastDeTime = Long.valueOf(mLatestDeliveryTimeEdt.getText().toString().trim());
//					}
//					if (StringUtils.isNoneNullString(mEarliestCommitTimeEdt.getText().toString().trim())) {
//						earlyComitTime = Long.valueOf(mEarliestCommitTimeEdt.getText().toString().trim());
//					}
//					if (StringUtils.isNoneNullString(mLatestCommitTimeEdt.getText().toString().trim())) {
//						lastComitTime = Long.valueOf(mLatestCommitTimeEdt.getText().toString().trim());
//					}
//
//					if (lastDeTime == 0) {
//						if (lastComitTime == 0) {
//							mEarliestCommitTimeLayout.setStateNormal();
//							isEarliestComTime = true;
//						} else {
//							if (earlyComitTime >= lastComitTime) {
//								mEarliestCommitTimeLayout.setStateWrong("最晚时间要大于最早时间");
//								isEarliestComTime = false;
//							} else {
//								mEarliestCommitTimeLayout.setStateNormal();
//								isEarliestComTime = true;
//								if (mLatestCommitTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//									mLatestCommitTimeLayout.setStateNormal();
//									isLatestComTime = true;
//								}
//								if (mLatestDeliveryTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//									mLatestDeliveryTimeLayout.setStateNormal();
//									isLatestDeTime = true;
//								}
//							}
//						}
//					} else {
//						if (earlyComitTime <= lastDeTime) {
//							//报错
//							mEarliestCommitTimeLayout.setStateWrong("交付时间要大于发货时间");
//							isEarliestComTime = false;
//						} else {
//							//再比较和最晚时间
//							if (mLatestDeliveryTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//								mLatestDeliveryTimeLayout.setStateNormal();
//								isLatestDeTime = true;
//							}
//							if (lastComitTime == 0) {
//								mEarliestCommitTimeLayout.setStateNormal();
//								isEarliestComTime = true;
//								if (mLatestDeliveryTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//									mLatestDeliveryTimeLayout.setStateNormal();
//									isLatestDeTime = true;
//								}
//							} else {
//								if (earlyComitTime < lastComitTime) {
//									mEarliestCommitTimeLayout.setStateNormal();
//									isEarliestComTime = true;
//									if (mLatestCommitTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//										mLatestCommitTimeLayout.setStateNormal();
//										isLatestComTime = true;
//									}
//									if (mLatestDeliveryTimeLayout.getStateChar().equals("交付时间要大于发货时间")) {
//										mLatestDeliveryTimeLayout.setStateNormal();
//										isLatestDeTime = true;
//									}
//								} else {
//									mEarliestCommitTimeLayout.setStateWrong("最晚时间要大于最早时间");
//									isEarliestComTime = false;
//								}
//							}
//
//						}
//					}
//					checkCanClick();
//				} catch (NumberFormatException e) {
//					e.printStackTrace();
//				}

			}
		});

		mLatestCommitTimeEdt.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				checkCanClick();

//				try {
//					if (StringUtils.isNullString(s.toString())) {
//						mLatestCommitTimeLayout.setStateNormal();
//						isLatestComTime = false;
//						checkCanClick();
//						return;
//					} else {
//						if (Long.valueOf(s.toString()) == 0) {
//							mLatestCommitTimeLayout.setStateWrong("发布时间不能为0");
//							isLatestComTime = false;
//							checkCanClick();
//							return;
//						}
//					}
//
//					long earComitTime = 0;
//					long lateComitTime = 0;
//					if (StringUtils.isNoneNullString(mEarliestCommitTimeEdt.getText().toString().trim())) {
//						earComitTime = Long.valueOf(mEarliestCommitTimeEdt.getText().toString().trim());
//					}
//					if (StringUtils.isNoneNullString(mLatestCommitTimeEdt.getText().toString().trim())) {
//						lateComitTime = Long.valueOf(mLatestCommitTimeEdt.getText().toString().trim());
//					}
//					if (earComitTime == 0) {
//						mLatestCommitTimeLayout.setStateNormal();
//						isLatestComTime = true;
//					} else {
//						if (earComitTime >= lateComitTime) {
//							mLatestCommitTimeLayout.setStateWrong("最晚时间要大于最早时间");
//							isLatestComTime = false;
//						} else {
//							mLatestCommitTimeLayout.setStateNormal();
//							isLatestComTime = true;
//							if (mEarliestCommitTimeLayout.getStateChar().equals("最晚时间要大于最早时间")) {
//								mEarliestCommitTimeLayout.setStateNormal();
//								isEarliestComTime = true;
//							}
//						}
//					}
//
//					checkCanClick();
//				} catch (NumberFormatException e) {
//					e.printStackTrace();
//				}

			}
		});
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ReleaseDeliveryItemFragment) {
			mActivity.setTitle("新增交付项");
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null) {
			isCreate = getArguments().getBoolean(ContractConstant.KEY_BOOLEAN_DELIVERY_ITEM_IS_CREATE);
		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_DELIVERY_ITEM_PBUKID) != null) {
			pbUkid = getArguments().getString(ContractConstant.KEY_DELIVERY_ITEM_PBUKID);
		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID) != null) {
			mContractUkid = getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID);
		}

		if (StringUtils.isNoneNullString(getArguments().getString("type")) && getArguments().getString("type").equals(ContractConstant.RELEASE_SUPPLY_FUNCTION_EDIT)) {
			Map<String, Object> paramsMap = new HashMap<>();
			paramsMap.put("contractUkid", mContractUkid);
			paramsMap.put("type", 2);
			httpPost(ContractConstant.MEHOTD_MODIFY_NEW_DELIVER_ITEM_UPDATE, paramsMap, 3);
			return;
		}
		if (!isCreate) {
			Map<String, Object> map = new HashMap<>();
			map.put("contractUkid", pbUkid);
			map.put("type", 1);
			httpPost(ContractConstant.MEHOTD_MODIFY_NEW_DELIVER_ITEM_UPDATE, map, 1, false, "");
		}


	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (what == 1 || what == 3) {
			List<DeliverableItemBean.DataBean> deliverableItemBean = JSON.parseArray(commonClass.getData().toString(), DeliverableItemBean.DataBean.class);
			if (deliverableItemBean != null && deliverableItemBean.size() > 0) {
				DeliverableItemBean.DataBean dataBean = deliverableItemBean.get(0);
				if (dataBean != null) {
					//设置数据
					if (StringUtils.isNoneNullString(dataBean.getPbSendEarly())) {
						mEarliestDeliveryTimeEdt.setText(dataBean.getPbSendEarly());
						mEarliestDeliveryTimeLayout.setStateNormal();
					}
					if (StringUtils.isNoneNullString(dataBean.getPbSendLast())) {
						mLatestDeliveryTimeEdt.setText(dataBean.getPbSendLast());
						mLatestDeliveryTimeLayout.setStateNormal();
					}
					if (StringUtils.isNoneNullString(dataBean.getPbDeliveryEarly())) {
						mEarliestCommitTimeEdt.setText(dataBean.getPbDeliveryEarly());
						mEarliestCommitTimeLayout.setStateNormal();
					}
					if (StringUtils.isNoneNullString(dataBean.getPbDeliveryLast())) {
						mLatestCommitTimeEdt.setText(dataBean.getPbDeliveryLast());
						mLatestCommitTimeLayout.setStateNormal();
					}
					if (StringUtils.isNoneNullString(dataBean.getDeliveryUkid())) {
						deliveryTempUkid = dataBean.getDeliveryUkid();
					}
				}
			}
		} else if (what == 2) {
			if (commonClass.getCode().equals("0")) {
				DeliveryItemEvent deliveryItemEvent = new DeliveryItemEvent();
				deliveryItemEvent.setComitTime(mEarliestDeliveryTimeEdt.getText().toString().trim() + "天" + "-" + mLatestDeliveryTimeEdt.getText().toString().trim() + "天");
				deliveryItemEvent.setDeviTime(mEarliestCommitTimeEdt.getText().toString().trim() + "天" + "-" + mLatestCommitTimeEdt.getText().toString().trim() + "天");
				EventBus.getDefault().post(deliveryItemEvent);
				popFragment();

			} else {
				if (StringUtils.isNoneNullString(commonClass.getMsg())) {
					toast(commonClass.getMsg());
				}

			}
		} else if (what == 4) {
			if (commonClass.getCode().equals("0")) {
				popFragment();
				EventBus.getDefault().post(new RefreshPublishGoodsEvent("refresh"));
			} else {
				if (StringUtils.isNoneNullString(commonClass.getMsg())) {
					toast(commonClass.getMsg());
				}

			}
		}


	}

	@Override
	public void onReLoad(int code) {
		requestDatas();
	}

	@Override
	public void onClick(View v) {
		if (v == mConfirmBtn) {
			//校验开始
			//不能为0
			long earlyDetime = Long.valueOf(mEarliestDeliveryTimeEdt.getText().toString());
			long latestDetime = Long.valueOf(mLatestDeliveryTimeEdt.getText().toString());
			long earlyCommitTime = Long.valueOf(mEarliestCommitTimeEdt.getText().toString());
			long latestCommitTime = Long.valueOf(mLatestCommitTimeEdt.getText().toString());

			if (earlyDetime == 0 || latestDetime == 0 || earlyCommitTime == 0 || latestCommitTime == 0) {
				toast(R.string.string_contract_published_time_cannot_zero_title);
				return;
			}
			if (latestDetime >= earlyCommitTime) {
				toast(R.string.string_contract_commit_time_need_big_de_time_title);
				return;
			}
			if (earlyDetime >= latestDetime) {
				toast(R.string.string_contract_latest_commit_big_early_commit);
				return;
			}
			if (earlyCommitTime >= latestCommitTime) {
				toast(R.string.string_contract_latest_detime_big_early_detime);
				return;
			}
			Map<String, Object> paramsMap = new HashMap<>();
			paramsMap.put("pbSendEarly", mEarliestDeliveryTimeEdt.getText().toString());
			paramsMap.put("pbSendLast", mLatestDeliveryTimeEdt.getText().toString());
			paramsMap.put("pbDeliveryEarly", mEarliestCommitTimeEdt.getText().toString());
			paramsMap.put("pbDeliveryLast", mLatestCommitTimeEdt.getText().toString());
			paramsMap.put("deliveryUkid", deliveryTempUkid);
			if (StringUtils.isNoneNullString(getArguments().getString("type"))) {
				if (getArguments().getString("type").equals(ContractConstant.RELEASE_SUPPLY_FUNCTION_EDIT)) {
					paramsMap.put("contractUkid", mContractUkid);
					paramsMap.put("type", "2");
					httpPost(ContractConstant.MODIFY_GET_DELIVER_ITEM_METHOD, paramsMap, 4, true, "保存中...");
					return;
				}
			}
			paramsMap.put("contractUkid", pbUkid);
			paramsMap.put("type", "1");
			httpPost(ContractConstant.MODIFY_GET_DELIVER_ITEM_METHOD, paramsMap, 2, true, "保存中...");
		}
	}
}
